/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uet.model;

import edu.stanford.nlp.ling.CoreAnnotations;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 *
 * @author thinhnk
 */
public class Thread {
    String url;
    QuestionPost firstPost;
    List<Post> posts;
    public static void main(String[] args){
        long now = System.currentTimeMillis();
        Thread thread = new Thread("data/xml/thread_90.xml");
        System.out.println("TIME = " + (System.currentTimeMillis() - now));
    }
    public Thread(String fileName){
        try {
            posts = new ArrayList<Post>();
            File xmlFile = new File(fileName);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            //parse URL
            url = doc.getElementsByTagName("url").item(0).getTextContent();
            //parse POSTs
            //First post
            String title = doc.getElementsByTagName("title").item(0).getTextContent();
            NodeList allPosts = doc.getElementsByTagName("post");
            Element e = (Element) allPosts.item(0);
            firstPost = new QuestionPost(title, 0, e);
            for(int i = 1; i < allPosts.getLength(); i++){
                e = (Element) allPosts.item(i);
                posts.add(new Post(i, e));
            }     
            System.out.println(firstPost.toString());
        } catch (SAXException ex) {
            Logger.getLogger(Thread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Thread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Thread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
